<template>
  <van-popup
    :value="$store.state.channel.isShowPopup"
    @click-close-icon="$store.commit('channel/hidePopup')"
    closeable
    position="left"
    @closed="isEdit=false"
  >
   <div class="article-channel">
   <div class="head">
     <h3>我的频道<small>点击进入频道</small></h3>
     <a class="edit" href="javascript:;" @click="isEdit=!isEdit"
     :class="{active:isEdit}">{{ isEdit?'完成':'编辑' }}</a>
   </div>
   <div class="body">
     <a href="javascript:;" v-for="i in $store.getters.myChannels"
     :class="{active:i.id===$store.getters.activeChannelId}"
     :key="i.id" @click="changeChannel(i.id)"
     ref="myChannel"
     >
     {{ i.name }}
     <i v-show="isEdit && i.id !== 0 && i.id !== $store.state.channel.activeChannelId"
      class="iconfont icon-tag-close" @click.stop="delChannel(i.id)"></i>
    </a>
   </div>
   <div class="head" style="margin-top:12px">
     <h3>频道推荐</h3>
   </div>
   <div class="body">
     <a href="javascript:;" v-for="i in getOptionsChannels"
     :key="i.id" @click="addChannels(i)">
       {{ i.name }}
      </a>
   </div>
 </div>
  </van-popup>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'ArticleChannelPopup',
  data () {
    return {
      isEdit: false
    }
  },
  computed: {
    ...mapGetters('channel', ['getOptionsChannels'])
  },
  async created () {
    const [err] = await this.$store.dispatch('channel/loadAllChannels')
    if (err) return this.$toast.fail('获取所有频道失败')
  },
  methods: {
    changeChannel (id) {
      if (this.isEdit) return
      this.$store.commit('channel/setActiveChannelId', id)
      this.$store.commit('channel/hidePopup')
    },
    async addChannels (item) {
      const [err] = await this.$store.dispatch('channel/addChannel', item)
      if (err) this.$toast.fail('添加失败')
    },
    async delChannel (id) {
      const [err] = await this.$store.dispatch('channel/delChannel', id)
      if (err) this.$toast.fail('删除失败，请重试')
    }
  }
}
</script>

  <style scoped lang="less">
 .article-channel {
  margin-top: 44px;
  .head {
    padding: 0 16px;
    display: flex;
    justify-content: space-between;
    justify-items: center;
    padding-bottom: 12px;
    h3 {
      font-size: 16px;
      color: #333;
      margin: 0;
      small {
        font-size: 12px;
        color: #999;
        margin-left: 10px;
      }
    }
    .edit {
      float: right;
      height: 22px;
      width: 52px;
      line-height: 22px;
      text-align: center;
      color: #DE644B;
      border-radius: 11px;
      border: 1px solid #DE644B;
      font-size: 12px;
      &.active {
        color: #fff;
        background: #DE644B;
      }
    }
  }
  .body {
    padding: 0 6px 0 16px;
    a {
      position: relative;
      display: inline-block;
      padding: 0 8px;
      font-size:14px;
      color: #3A3948;
      background: #F7F8FA;
      height: 36px;
      line-height: 36px;
      min-width: 78px;
      margin-right: 10px;
      margin-bottom: 12px;
      border-radius: 18px;
      text-align: center;
    .icon-tag-close {
      position: absolute;
      top: -5px;
      right: -5px;
      line-height: 1;

      &.active {
        color: @geek-color;
      }
    }
  }
}
}
</style>
